<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆套餐</span>
  </div>
  <el-dialog :title="dialogTitle" v-model="dialogVisible" :before-close="handleClose"  style="width:500px;padding:40px;"  >
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="套餐名称">
            <el-input placeholder="请输入内容" v-model="saveVehicleComboForm.name"></el-input>
          </el-form-item>
          <el-form-item label="套餐价格">
            <el-input placeholder="请输入内容" v-model="saveVehicleComboForm.price"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button  @click="handleClose" >取消</el-button>
      <el-button type="primary" @click="saveVehicleCombo" >确定</el-button>
    </template>
  </el-dialog>
  <!-- 套餐搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="套餐名称">
        <el-input placeholder="请输入内容" v-model="searchVehicleComboForm.name" style="width:220px;" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resteSearch">重置</el-button>
        <el-button type="primary" @click="loadVechicleCombo">查询</el-button>
      </el-form-item>
      <el-form-item style="float: right">
        <el-button @click="dialogVisible=true" type="primary">新建套餐</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="vehicleComboArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="name" label="套餐名称" align="center"/>
      <el-table-column prop="price" label="套餐价格" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small"  @click="editVehicleCombo(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small"  @click="deleteVehicleCombo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
//控制新增和修改窗口的名称和显示
import {onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
import qs from "qs";

const dialogVisible = ref(false);
const dialogTitle = ref('新增套餐');
//保存查询到的套餐信息
const vehicleComboArr=ref([]);
const searchVehicleComboForm=ref({name:''});
const loadVechicleCombo=()=>{
  let data=qs.stringify(searchVehicleComboForm.value);
  axios.get(BASE_URL+'/v1/combo/select?'+data).then((response)=>{
    if(response.data.code==2000){
      vehicleComboArr.value=response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
onMounted(()=>{
  loadVechicleCombo();//已进入页面就加载套餐
})
//重置搜索信息
const resteSearch=()=>{
  searchVehicleComboForm.value={};
  loadVechicleCombo();
}

//新增套餐信息
const saveVehicleComboForm=ref({
  name:'',
  price:''
})
const saveVehicleCombo=()=>{
  let data=qs.stringify(saveVehicleComboForm.value);
  axios.post(BASE_URL+'/v1/combo/save',data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success('保存成功');
      dialogVisible.value=false;
      saveVehicleComboForm.value={};
      loadVechicleCombo();
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
const handleClose=()=>{
  if(confirm('确认关闭本窗口吗？')){
    dialogVisible.value=false;
    saveVehicleComboForm.value={};
  }
}
//修改套餐信息
const editVehicleCombo=(id)=>{
  dialogTitle.value="编辑套餐";
  dialogVisible.value=true;
  axios.get(BASE_URL+"/v1/combo/select?id="+id).then((response)=>{
    if(response.data.code==2000){
      saveVehicleComboForm.value=response.data.data[0];
      loadVechicleCombo();
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//删除车辆
const deleteVehicleCombo=(id)=>{
  if(confirm('您确认要删除吗?')){
    axios.post(BASE_URL+"/v1/combo/delete/"+id).then((response)=>{
      if(response.data.code==2000){
        ElMessage.success("删除成功!");
        loadVechicleCombo();
      }else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}
</script>

<style scoped>

</style>